package top.ywlog.permission.filter;

import org.springframework.core.annotation.Order;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author: Durian
 * @date: 2020/10/18 3:24
 * @description:
 */
@Order(5)
@WebFilter(filterName = "requestBodyFilter", urlPatterns = "/*",
        initParams = {
        @WebInitParam(name = "exclusions", value = "/druid/*")
})
public class RequestBodyFilter extends OncePerRequestFilter
{
    private static final String PATH = "/druid/";

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
            throws ServletException, IOException
    {
        // 对druid的请求将不做处理
        if (request.getRequestURI() != null && request.getRequestURI().contains(PATH))
        {
            filterChain.doFilter(request, response);
        } else
        {
            // 防止流读取一次后就没有了, 所以需要将流继续写出去
            ServletRequest requestWrapper = new RequestWrapper(request);
            //最终的塞的操作
            filterChain.doFilter(requestWrapper, response);
        }
    }
}
